// pages/mine_detail/classshoucang/shoucang.js
Page({
  data: {
    activeTab: 0,
    courseList: [],
    loading: false
  },

  onLoad() {
    this.loadCourseList();
  },

  // 切换标签页
  switchTab(e) {
    const index = parseInt(e.currentTarget.dataset.index);
    
    // 如果点击的是当前标签，不做处理
    if (this.data.activeTab === index) return;
    
    this.setData({ 
      activeTab: index,
      courseList: []  // 清空当前列表
    }, () => {
      this.loadCourseList();  // 加载新的列表数据
    });
  },

  // 加载课程列表
  loadCourseList() {
    const user = wx.getStorageSync('user');
    if (!user || !user.userId) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      return;
    }

    this.setData({ loading: true });
    
    // 根据activeTab选择不同的接口
    const url = this.data.activeTab === 0 ? 
      'http://120.26.112.194:80/api/admin/courses/collectionChecked' : 
      'http://120.26.112.194:80/api/admin/courses/collectionNoChecked';

    wx.request({
      url: `${url}?userId=${user.userId}`,
      method: 'GET',
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({ 
            courseList: res.data.data || [],
            loading: false
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '加载失败',
          icon: 'none'
        });
        this.setData({ loading: false });
      }
    });
  },

  // 跳转到课程详情
  goToDetail(e) {
    const courseId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/kecheng_detail/kechengxiangqing/kechengxiangqing?id=${courseId}`
    });
  },

  onPullDownRefresh() {
    this.loadCourseList();
    wx.stopPullDownRefresh();
  }
});